package meiRiYiTi;

import java.awt.*;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Author: 笨蛋超人不会飞
 * Date:  9:20
 */
public class day_826 {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        //在排序之前先用hash把difficulty和profit的关联关系存储起来
        HashMap<Integer,Integer> hashMap=new HashMap<>();
        for (int i = 0; i < profit.length; i++) {
            int temp=0;
            if(hashMap.get(profit[i])!=null){
                temp=hashMap.get(profit[i]);
                hashMap.put(profit[i],difficulty[i]<temp?difficulty[i]:temp);
                continue;
            }
            hashMap.put(profit[i],difficulty[i]);
        }
        //对profit进行排序
        Arrays.sort(profit);
        int result=0;
        for (int i = 0; i < worker.length; i++) {
            for (int j = profit.length-1; j >=0 ; j--) {
                if(worker[i]>=hashMap.get(profit[j])){
                    result+=profit[j];
                    break;
                }
            }
        }
        return result;
    }
    public int maxProfitAssignment_01(int[] difficulty, int[] profit, int[] worker) {
        int N = difficulty.length;
        Point[] jobs = new Point[N];
        for (int i = 0; i < N; ++i){
            jobs[i] = new Point(difficulty[i], profit[i]);
        }
        Arrays.sort(jobs, (a, b) -> a.x - b.x);
        Arrays.sort(worker);

        int ans = 0, i = 0, best = 0;
        for (int skill: worker) {
            while (i < N && skill >= jobs[i].x){
                best = Math.max(best, jobs[i++].y);
            }
            ans += best;
        }

        return ans;

    }



    public static void main(String[] args) {
//        difficulty =
//[2,17,19,20,24,29,33,43,50,51,57,67,70,72,73,75,80,82,87,90]
//        profit =
//[6,7,10,17,18,29,30,31,34,39,40,42,48,54,57,78,78,78,83,88]
//        worker =
//[12,9,11,41,11,87,48,6,48,93,76,73,7,50,55,97,47,33,46,10]
//        按照上面生成三个数组
        int [] difficulty = {2,17,19,20,24,29,33,43,50,51
                ,57,67,70,72,73,75,80,82,87,90
        };
        int [] profit = {6,7,10,17,18,29,30,31,34,39,40,42,48,54,57,78,78,78,83,88};
        int [] worker = {12,9,11,41,11,87,48,6,48,93,76,73,7,50,55,97,47,33,46,10};
        day_826 day_826 = new day_826();
        System.out.println(day_826.maxProfitAssignment(difficulty,profit,worker));

    }

}
